﻿using DotNet.Highcharts;
using DotNet.Highcharts.Helpers;
using DotNet.Highcharts.Options;
using StockAnalyzerApp.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;

namespace StockAnalyzerApp.Services.Impl
{
    public class GraphService : IGraphService
    {
        public Highcharts updateGraph(String dateRangeType, String industrySymbol1, String industrySymbol2, String fromDate, String toDate)
        {
            GraphDataService graphDataService = new GraphDataService();
            PercentChangeDataService percentChangeDataService = new PercentChangeDataService();

            //Get Data for Line 1
            List<DataItem> dataItemList1 = graphDataService.getDataSet(dateRangeType, industrySymbol1, fromDate, toDate);       
            List<PercentChangeDataItem> percentChangeDataItemList1 = percentChangeDataService.toPercentChangeDataList(dataItemList1);
            double[] percentArray1 = (from t in percentChangeDataItemList1 select t.PercentChange).ToArray<double>();

            object[] xItemArray1 = new object[percentArray1.Length];
            for (int i = 0; i < percentArray1.Length; i++)
            {
                xItemArray1[i] = percentArray1[i];
            }

            //Get Data for Line 1
            List<DataItem> dataItemList2 = graphDataService.getDataSet(dateRangeType, industrySymbol2, fromDate, toDate);
            List<PercentChangeDataItem> percentChangeDataItemList2 = percentChangeDataService.toPercentChangeDataList(dataItemList2);
            double[] percentArray2 = (from t in percentChangeDataItemList2 select t.PercentChange).ToArray<double>();

            object[] xItemArray2 = new object[percentArray2.Length];
            for (int i = 0; i < percentArray2.Length; i++)
            {
                xItemArray2[i] = percentArray2[i];
            }

            //Get Date Range
            String[] dateRangeArray = (from t in percentChangeDataItemList1 select t.DateRange).ToArray<String>();
            
            string[] categories = new string[dateRangeArray.Length];
            for (int i = 0; i < dateRangeArray.Length; i++)
            {
                categories[i] = dateRangeArray[i];
            }

            //Define Series Array
            Series[] seriesArray = new Series[2];
            seriesArray[0] = new Series
               {
                   Name = industrySymbol1,
                   Data = new Data(xItemArray1)
               };
            seriesArray[1] = new Series
               {
                   Name = industrySymbol2,
                   Data = new Data(xItemArray2)
               };

            //Define Colors for Series line
            Color[] colors = new Color[2];
            colors[0] = ColorTranslator.FromHtml("#7cb5ec");
            colors[1] = ColorTranslator.FromHtml("#8c0095");

            //Define the Chart
            Highcharts chart = new DotNet.Highcharts.Highcharts("chart")
            .SetTitle(new Title
            {
                Text = "Stock Price Percentage Change Trending",
                X = -20
            })
           .SetXAxis(new XAxis
           {
               Categories = categories           
           })
           .SetYAxis(new YAxis
           {
               Title = new YAxisTitle
               {
                   Text = "Percentage Change (%)"
               }
           })
           .SetSeries(seriesArray)
           .SetTooltip(new Tooltip
           {
               UseHTML = true,
               Shared = true,
               HeaderFormat = "<span style=\"color:orange\">Date Range:</span>{point.key}<br>",
               PointFormat = "<span style=\"color:{series.color}\">\u25CF</span><span style=\"color:{series.color}\">{series.name}</span>: <b>{point.y}%</b><br/>"
           })
           ;

            return chart;
        }
    }
}